package com.yyfax.common.recyclerview.wrapper;

import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.yyfax.common.recyclerview.adapter.MultiItemTypeAdapter;
import com.yyfax.common.recyclerview.base.ViewHolder;
import com.yyfax.common.recyclerview.view.IRecyclerView;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * Created by HenryZhang on 2017/3/16
 * modified by fuqiang on 2017/5/17
 * 加载后是空，错误状态
 * Copyright © 2017 www.yylending.com. All Rights Reserved
 */
public class StatesAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private IRecyclerView mRecyclerView;

    private RecyclerView.Adapter mAdapter;

    public interface RetryClickListener {
        void onRetry();
    }

    protected RetryClickListener mListener;


    @IntDef({STATE_NORMAL, STATE_EMPTY, STATE_ERROR, STATE_LOADING})
    @Retention(RetentionPolicy.SOURCE)
    public @interface State {
    }

    public static final int STATE_NORMAL = 0;
    public static final int STATE_EMPTY = -1;
    public static final int STATE_ERROR = -2;
    public static final int STATE_LOADING = -3;

    @State
    private int state = STATE_NORMAL;

    public StatesAdapter(@NonNull MultiItemTypeAdapter adapter, IRecyclerView view) {
        mRecyclerView = view;
        init(adapter);
    }

    public void init(@NonNull MultiItemTypeAdapter adapter) {
        mAdapter = adapter;
        mAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            public void onChanged() {
                notifyDataSetChanged();
            }

            public void onItemRangeChanged(int positionStart, int itemCount) {
                notifyItemRangeChanged(positionStart, itemCount);
            }

            public void onItemRangeInserted(int positionStart, int itemCount) {
                notifyItemRangeInserted(positionStart, itemCount);
            }

            public void onItemRangeRemoved(int positionStart, int itemCount) {
                notifyItemRangeRemoved(positionStart, itemCount);
            }

            public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
                notifyItemMoved(fromPosition, toPosition);
            }
        });
    }

    @State
    public int getState() {
        return state;
    }

    public void setState(@State int state) {
        if (state == STATE_NORMAL) {
            mRecyclerView.showLoadMoreView(true);
        } else {
            mRecyclerView.showLoadMoreView(false);
        }
//        if (mAdapter.getItemCount() == 0)//如果没有数据，不能上拉加载更多
//            mRecyclerView.setLoadMoreEnabled(false);
        this.state = state;
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        switch (state) {
            case STATE_EMPTY:
            case STATE_ERROR:
			case STATE_LOADING:
                return 1;
        }
        return mAdapter.getItemCount();
    }

    @Override
    public int getItemViewType(int position) {
        switch (state) {
            case STATE_EMPTY:
            case STATE_ERROR:
			case STATE_LOADING:
                return state;
        }
        return mAdapter.getItemViewType(position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case STATE_EMPTY:
                return ViewHolder.createViewHolder(parent.getContext(), parent, mRecyclerView.getItemViewDelegateManager().getItemViewLayoutId(STATE_EMPTY));
            case STATE_ERROR:
                return ViewHolder.createViewHolder(parent.getContext(), parent, mRecyclerView.getItemViewDelegateManager().getItemViewLayoutId(STATE_ERROR));
			case STATE_LOADING:
				return ViewHolder.createViewHolder(parent.getContext(), parent, mRecyclerView.getItemViewDelegateManager().getItemViewLayoutId(STATE_LOADING));
        }
        return mAdapter.onCreateViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (state) {
            case STATE_EMPTY:
            case STATE_ERROR:
			case STATE_LOADING:
                onBindCustomViewHolder(holder);
                break;
            default:
                mAdapter.onBindViewHolder(holder, position);//如果是默认normal，那么交给wrap adapter去实现具体逻辑
                break;
        }
    }

    public void onBindCustomViewHolder(RecyclerView.ViewHolder holder) {
        if (state != STATE_EMPTY) {
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onRetry();
                }
            });
        }
    }

    public void setRetryClickListener(RetryClickListener listener) {
        mListener = listener;
    }

}
